package com.cskaoyan.service;

import com.cskaoyan.bean.aftersale.AfterSaleDetailVo;
import com.cskaoyan.bean.aftersale.AfterSaleWxVo;
import com.cskaoyan.bean.aftersale.AfterSaleWxVoVer2;
import com.cskaoyan.bean.baseresultVO.BaseData;
import com.cskaoyan.bean.baseresultVO.BaseParam;
import com.cskaoyan.bean.domain.*;
import com.cskaoyan.mapper.AfterSaleMapper;
import com.cskaoyan.mapper.OrderGoodsMapper;
import com.cskaoyan.mapper.OrderMapper;
import com.cskaoyan.utils.MyUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.tomcat.jni.Local;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AfterSaleServiceImpl implements AfterSaleService {
    @Resource
    AfterSaleMapper mapper;

    @Resource
    OrderMapper orderMapper;

    @Override
    public BaseData getList(BaseParam param, Short status, String aftersaleSn, Integer orderId) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        AfterSaleExample example = new AfterSaleExample();
        AfterSaleExample.Criteria criteria = example.createCriteria();
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }

        if (aftersaleSn != null && !aftersaleSn.trim().equals("")) {
            criteria.andAftersaleSnEqualTo(aftersaleSn.trim());
        }

        if (orderId != null) {
            criteria.andOrderIdEqualTo(orderId);
        }

        example.setOrderByClause(param.getSort() + " " + param.getOrder());

        List<AfterSale> afterSaleList = mapper.selectByExample(example);

        PageInfo<AfterSale> pageInfo = new PageInfo<>(afterSaleList);
        Integer total = MyUtils.getInteger(pageInfo.getTotal());
        return new BaseData(pageInfo.getPageNum(), pageInfo.getSize(), total, pageInfo.getPages(), afterSaleList);

    }

    @Override
    public void batchRecept(List<Integer> ids) {
        AfterSale afterSale = new AfterSale();
        AfterSale afterSale1;
        Integer orderId;
        for (Integer afterSaleId : ids) {
            afterSale.setId(afterSaleId);
            afterSale.setStatus((short) 2);
            LocalDateTime dateTime = LocalDateTime.now();
            afterSale.setHandleTime(dateTime);
            mapper.updateByPrimaryKeySelective(afterSale);

            afterSale1 = mapper.selectByPrimaryKey(afterSaleId);
            orderId = afterSale1.getOrderId();

            Order order = new Order();
            order.setId(orderId);
            order.setAftersaleStatus((short) 2);
            order.setUpdateTime(dateTime);
            orderMapper.updateByPrimaryKeySelective(order);
        }

    }

    @Override
    public void batchReject(List<Integer> ids) {
        AfterSale afterSale = new AfterSale();
        AfterSale afterSale1;
        Integer orderId;
        for (Integer afterSaleId : ids) {
            afterSale.setId(afterSaleId);
            afterSale.setStatus((short) 4);
            LocalDateTime dateTime = LocalDateTime.now();
            afterSale.setHandleTime(dateTime);
            mapper.updateByPrimaryKeySelective(afterSale);

            afterSale1 = mapper.selectByPrimaryKey(afterSaleId);
            orderId = afterSale1.getOrderId();

            Order order = new Order();
            order.setId(orderId);
            order.setAftersaleStatus((short) 4);
            order.setUpdateTime(dateTime);
            orderMapper.updateByPrimaryKeySelective(order);
        }
    }

    @Override
    public void refund(AfterSale afterSale) {
        Integer orderId;
        LocalDateTime dateTime = LocalDateTime.now();
        afterSale.setHandleTime(dateTime);
        afterSale.setStatus((short) 3);
        mapper.updateByPrimaryKeySelective(afterSale);
        orderId = afterSale.getOrderId();

        Order order = new Order();
        order.setId(orderId);
        order.setOrderStatus((short) 203);
        order.setAftersaleStatus((short) 3);
        order.setUpdateTime(dateTime);
        orderMapper.updateByPrimaryKeySelective(order);
    }


    @Override
    public void recept(AfterSale afterSale) {
        Integer orderId;
        LocalDateTime dateTime = LocalDateTime.now();

        afterSale.setHandleTime(dateTime);

        afterSale.setStatus((short)2);

        mapper.updateByPrimaryKeySelective(afterSale);
        orderId = afterSale.getOrderId();

        Order order = new Order();
        order.setId(orderId);
        order.setAftersaleStatus((short) 2);

        order.setUpdateTime(dateTime);

        orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public void reject(AfterSale afterSale) {
        Integer orderId;
        LocalDateTime dateTime = LocalDateTime.now();
        afterSale.setHandleTime(dateTime);
        mapper.updateByPrimaryKeySelective(afterSale);
        orderId = afterSale.getOrderId();

        Order order = new Order();
        order.setId(orderId);
        order.setAftersaleStatus((short) 4);
        order.setUpdateTime(LocalDateTime.now());
        orderMapper.updateByPrimaryKeySelective(order);
    }


    /**
     * 微信售后接口
     *
     * @param afterSale
     * @param order
     * @return
     */
    @Override
    public Integer getSubmit(AfterSale afterSale, Order order) {

        // TODO: 2021/12/5 当前订单改为202
        order.setOrderStatus((short) 202);
        //插入一个售后表
        mapper.insertSelective(afterSale);
        //更新一个订单表 根据订单id
        OrderExample orderExample = new OrderExample();
        OrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andIdEqualTo(afterSale.getOrderId());
        orderMapper.updateByExampleSelective(order, orderExample);
        return 200;
    }

    @Resource
    OrderGoodsMapper orderGoodsMapper;

    @Override
    public AfterSaleWxVo getListForWx(AfterSale afterSale, User user, BaseParam baseParam) {
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());

        //每个售后订单里有一个商品集合 一个人有一个集合的售后订单


        //查询status满足条件的售后表 以及这个用户下的所有订单号
        AfterSaleExample example = new AfterSaleExample();
        AfterSaleExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(afterSale.getStatus()).andUserIdEqualTo(user.getId());
        List<AfterSale> afterSales = mapper.selectByExample(example);

        //这个集合是最终的集合
        ArrayList<AfterSaleWxVoVer2> afterList = new ArrayList<>();
        //为订单集合里的每一个订单赋值以及每一个商品赋值
        AfterSaleWxVoVer2 afterSaleWxVoVer2 = null;
        for (int i = 0; i < afterSales.size(); i++) {
            afterSaleWxVoVer2 = new AfterSaleWxVoVer2();
            //每一条订单的信息都赋值过去
            afterSaleWxVoVer2.setAftersale(afterSales.get(i));
            //从ordergoods表找到这个订单号下的所有goods
            OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
            OrderGoodsExample.Criteria criteriaOrderGoods = orderGoodsExample.createCriteria();
            //根据查询到的售后单得到对应的orderid 得到对应下的所有商品
            criteriaOrderGoods.andOrderIdEqualTo(afterSales.get(0).getOrderId()).andDeletedEqualTo(false);
            List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);
            afterSaleWxVoVer2.setGoodsList(orderGoods);
            afterList.add(afterSaleWxVoVer2);
        }
        //放进vo
        AfterSaleWxVo afterSaleWxVo = new AfterSaleWxVo();
        afterSaleWxVo.setList(afterList);
        PageInfo<AfterSale> pageInfo = new PageInfo<>(afterSales);
        afterSaleWxVo.setLimit(pageInfo.getSize());
        afterSaleWxVo.setPage(pageInfo.getPageNum());
        afterSaleWxVo.setPages(pageInfo.getPages());
        afterSaleWxVo.setTotal(pageInfo.getTotal());
        return afterSaleWxVo;
    }

    @Override
    public AfterSaleDetailVo getDetailAftersaleForWx(Order order) {
        AfterSaleDetailVo afterSaleDetailVo = new AfterSaleDetailVo();
        Integer orderId = order.getId();
        //查询订单表 订单id的订单
        Order orderDetail = orderMapper.selectByPrimaryKey(orderId);
        //查询售后表下订单信息,一个
        AfterSaleExample afterSaleExample = new AfterSaleExample();
        afterSaleExample.createCriteria().andOrderIdEqualTo(orderId).andDeletedEqualTo(false);
        List<AfterSale> afterSales = mapper.selectByExample(afterSaleExample);
        //仅会对应一个所以返回第一个
        afterSaleDetailVo.setAftersale(afterSales.get(0));
        //查询对应商品
        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        orderGoodsExample.createCriteria().andOrderIdEqualTo(orderId).andDeletedEqualTo(false);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);

        afterSaleDetailVo.setOrder(orderDetail);
        afterSaleDetailVo.setOrderGoods(orderGoods);
        return afterSaleDetailVo;


    }


}
